package com.ludomancy.spaceopera.entities;
import java.awt.Graphics;
import java.awt.Point;
import com.ludomancy.gamelib.Accumulator;
import com.ludomancy.gamelib.ImageStrip;
import com.ludomancy.gamelib.ResourceManager;
import com.ludomancy.gamelib.Utils;
import com.ludomancy.spaceopera.Game;
import com.ludomancy.spaceopera.LandWorld;
import com.ludomancy.spaceopera.Player;


public class StormHazard extends Hazard
{	
	private ImageStrip shadowStrip;
	private Accumulator thruster = new Accumulator(Game.forTime(100));
	private Accumulator hitter = new Accumulator(Game.forTime(30));
	
	private static final int CLOUDS = 3;
	private int cloud;
	private static int lastType = 0;

	public StormHazard(Point pos)
	{
		super(pos);
		z = ZMIDDLE;
		shadowStrip = new ImageStrip(ResourceManager.getImage("stormshadow.png"), 40, 10);
		width = (int) proj.screenToWorldX(shadowStrip.frameWidth);
		height = (int) proj.screenToWorldY(shadowStrip.frameHeight);
		lastType = Math.abs(lastType+1);
		//TODO:WTF?!?!
		cloud = lastType % (CLOUDS - 1);
	}
	
	public boolean isConsumed()
	{
		return false;
	}
			
	public void paint(Graphics g, int x, int y)
	{
		Utils.pushAlpha(g, 0.5f);
		shadowStrip.paint(g, cloud, x, y);
		Utils.popAlpha(g);
	}
	
	public boolean update()
	{
		thruster.update();
		if(thruster.isDone())
		{
			pos.x -= 15;
			if(pos.x < -getWidth()/2)
			{
				pos.x = Game.WORLD_WIDTH+getWidth()/2;
			}
			thruster.reset();
		}
		return false;
	}
	
	public int[] getBitmask(int x, int y, int w, int h)
	{
		return shadowStrip.getBitmask(cloud, x, y, w, h);
	}
	
	public void onPlayerHit(Player player)
	{
		hitter.update();
		if(hitter.isDone())
		{
			player.hit(1);
			hitter.reset();
		}
	}

	public int getType()
	{
		return LandWorld.HAZARD_STORM;
	}
}
